package com.mbc.common.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public abstract class DAO<T> {
	protected String table_name;
	protected String query;
	protected Connection conn;
	protected PreparedStatement pstmt;
	protected ResultSet rs;
	
	protected String getSelectQuery()
	{
		return "select * from " + table_name;
	}
	
	protected String getDeleteQuery()
	{
		return "delete from " + table_name;
	}
	
	protected String insertParam(String tableName ,String... param) {
		String l_query = "INSERT INTO " + tableName + " ( ";
		String l_param = "";
		
		for(int i = 0; i < param.length; i++) {
			if(i == param.length - 1) {
				l_query += param[i] + " ) ";
				l_param += "? )";
			} else {
				l_query += param[i] + " ,";
				l_param += "?, ";
			}
		}
		
		return l_query + " VALUES ( " + l_param;
	}
	
	protected String updateParam(String tableName, String... param) {
		String l_query = "UPDATE " + tableName + " SET ";
		
		for(int i = 0; i < param.length; i++) {
			if(i == param.length - 1) {
				l_query += param[i] + " = ? ";
			} else {
				l_query += param[i] + " = ?,";
			}
		}
		
		return l_query + " WHERE 1 = 1 ";
	}
	
	protected abstract T readRecord(ResultSet rs)throws SQLException;
}
